﻿using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;

namespace WT.OpenIddict.Domain.Services;

public static class StringExtension
{
    public static string Sha256(this string input)
    {
        using (var sha256Hash = SHA256.Create())
        {
            // Convert the input string to a byte array and compute the hash.
            var bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Convert byte array to a string
            var builder = new StringBuilder();
            for (var i = 0; i < bytes.Length; i++) builder.Append(bytes[i].ToString("x2"));
            return builder.ToString();
        }
    }

    public static T DeepCopy<T>(this T obj)
    {
        var json = JsonConvert.SerializeObject(obj);
        return JsonConvert.DeserializeObject<T>(json);
    }
}